﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DA;

namespace ERPMVC.Controllers
{ 
    public class OrdersController : Controller
    {
        private dotNetivotEntities db = new dotNetivotEntities();

        //
        // GET: /Orders/

        public ViewResult Index()
        {
            var erp_orders = db.ERP_Orders.Include("ERP_Suppliers").Include("ERP_Users");
            return View(erp_orders.ToList());
        }

        //
        // GET: /Orders/Details/5

        public ViewResult Details(int id)
        {
            ERP_Orders erp_orders = db.ERP_Orders.Single(e => e.OrderId == id);
            return View(erp_orders);
        }

        //
        // GET: /Orders/Create

        public ActionResult Create()
        {
            ViewBag.SupplierId = new SelectList(db.ERP_Suppliers, "SupplierId", "Name");
            ViewBag.UserId = new SelectList(db.ERP_Users, "UserId", "Password");
            return View();
        } 

        //
        // POST: /Orders/Create

        [HttpPost]
        public ActionResult Create(ERP_Orders erp_orders)
        {
            if (ModelState.IsValid)
            {
                db.ERP_Orders.AddObject(erp_orders);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.SupplierId = new SelectList(db.ERP_Suppliers, "SupplierId", "Name", erp_orders.SupplierId);
            ViewBag.UserId = new SelectList(db.ERP_Users, "UserId", "Password", erp_orders.UserId);
            return View(erp_orders);
        }
        
        //
        // GET: /Orders/Edit/5
 
        public ActionResult Edit(int id)
        {
            ERP_Orders erp_orders = db.ERP_Orders.Single(e => e.OrderId == id);
            ViewBag.SupplierId = new SelectList(db.ERP_Suppliers, "SupplierId", "Name", erp_orders.SupplierId);
            ViewBag.UserId = new SelectList(db.ERP_Users, "UserId", "Password", erp_orders.UserId);
            return View(erp_orders);
        }

        //
        // POST: /Orders/Edit/5

        [HttpPost]
        public ActionResult Edit(ERP_Orders erp_orders)
        {
            if (ModelState.IsValid)
            {
                db.ERP_Orders.Attach(erp_orders);
                db.ObjectStateManager.ChangeObjectState(erp_orders, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.SupplierId = new SelectList(db.ERP_Suppliers, "SupplierId", "Name", erp_orders.SupplierId);
            ViewBag.UserId = new SelectList(db.ERP_Users, "UserId", "Password", erp_orders.UserId);
            return View(erp_orders);
        }

        //
        // GET: /Orders/Delete/5
 
        public ActionResult Delete(int id)
        {
            ERP_Orders erp_orders = db.ERP_Orders.Single(e => e.OrderId == id);
            return View(erp_orders);
        }

        //
        // POST: /Orders/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            ERP_Orders erp_orders = db.ERP_Orders.Single(e => e.OrderId == id);
            db.ERP_Orders.DeleteObject(erp_orders);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}